package com.cowcode.controller;

import com.cowcode.biz.CollectBiz;
import com.cowcode.biz.QuestionBiz;
import com.cowcode.biz.StudyBiz;
import com.cowcode.biz.TextBiz;
import com.cowcode.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * User: ljy
 * Date: 2021/6/27
 * Time: 11:08
 */
@Controller
@RequestMapping("faq/")
public class FaqController {
    @Resource
    private QuestionBiz questionBiz;
    @Resource
    private TextBiz textBiz;
    @Resource
    private StudyBiz studyBiz;
    @Resource
    private CollectBiz collectBiz;

    @RequestMapping(value = "search",method = RequestMethod.POST)
    public String search(Model model,String uname, String text, Integer sid, Integer page,HttpSession session){
        List<Question> qlist = null;
        if ("".equals(text)){
            qlist = questionBiz.query6();
        } else {
            qlist = questionBiz.queryTitle(uname,text,sid,page);
        }
        List<Text> alist = textBiz.queryAnswer();
        model.addAttribute("qlist",qlist);
        model.addAttribute("alist",alist);
        List<Collect> clist = null;//收藏夹的集合
        User user = (User) session.getAttribute("user");
        if (user!=null){
            clist =  collectBiz.queryByUid(user.getUid());
        }
        model.addAttribute("clist",clist);
        return "tiaozhuan";

//        String str = "";
//        for (int i=0;i<qlist.size();i++){
//            str+="<div class='_sin98ufaq'><h4>"+qlist.get(i).getQtext()+"<span style='float: right'>提问人："+
//                    qlist.get(i).getUid().getUname()+"。时间"+qlist.get(i).getQsend()+"</span></h4>";
//            for (int i1=0;i1<alist.size();i1++){
//                if (alist.get(i1).getUid().getUid()==qlist.get(i).getUid().getUid()){
//                    str+="<p>"+alist.get(i1).getTtext()+"</p>";
//                }
//            }
//            str+="</div>";
//        }
    }

    @ResponseBody
    @RequestMapping(value = "sendquestion",method = RequestMethod.POST)
    public int sendquestion(String qtext, Integer sid, HttpSession session){
        Question question = new Question();
        question.setQtext(qtext);
        User user = (User) session.getAttribute("user");
        question.setUid(user);
        Study study = new Study();
        study.setSid(sid);
        question.setSid(study);
        return questionBiz.sendQuestion(question)?1:0;
    }
    @RequestMapping("answerQuestion")
    public String answerQuestion(HttpSession session,String text,Integer qid,Model model){
        User user = (User) session.getAttribute("user");
        Text text1 = new Text();
        text1.setUid(user);
        text1.setTtext(text);
        text1.setQid(qid);
        if (textBiz.answerQuestion(text1)){
            model.addAttribute("msg","回复成功");
        } else {
            model.addAttribute("msg","回复失败");
        }
        return "redirect:/jump/tofaq";
    }
    @RequestMapping("page/{index}")
    public String index(@PathVariable("index") int index,Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        int pagesize = 6;//每页显示6条
        int count = questionBiz.getCount();//总条数
        int pagecount = count % pagesize == 0 ? (count / pagesize) : (count / pagesize + 1);//总页码
        //页面向下容错
        index = index < 1 ? 1 : index;
        //向上容错
        index = index > pagecount ? pagecount : index;
        List<Question> qlist = questionBiz.queryByPage((index-1)*6, pagesize);
        model.addAttribute("pagecount",pagecount);//总页数
        model.addAttribute("count",count);//总条数
        model.addAttribute("qlist",qlist);//问题集合
        List<Text> alist = new ArrayList<>();//回复
        if (qlist.size()>0){
            for (Question question : qlist) {
                List<Text> textList = textBiz.queryByQid(question.getQid());
                for (Text text : textList) {
                    alist.add(text);
                }
            }
        }
        model.addAttribute("alist",alist);
        List<Study> slist = studyBiz.queryAll();//语言
        model.addAttribute("slist",slist);
        String index2 = Integer.toBinaryString(index);
        String pagecount2 = Integer.toBinaryString(pagecount);
        model.addAttribute("index2",index2);
        model.addAttribute("pagecount2",pagecount2);
        model.addAttribute("jump","tofaq");
        String msg = (String) model.getAttribute("msg");
        model.addAttribute("msg",msg);
        List<Collect> clist = null;//收藏夹的集合
        if (user!=null){
            clist =  collectBiz.queryByUid(user.getUid());
        }
        model.addAttribute("clist",clist);
        System.out.println("clist = " + clist);
        return "faq";
    }
    @RequestMapping("deleteReview/{qid}")
    public String deleteReview(@PathVariable("qid") Integer qid,Model model){
        model.addAttribute("jump","tofaq");
        if (questionBiz.deleteByQid(qid)) {
            textBiz.deleteByQid(qid);
            model.addAttribute("msg","删除问题成功");
        } else {
            model.addAttribute("msg","删除问题失败");
        }
        return "redirect:/jump/tofaq";
    }
    @RequestMapping("deleteAnswer/{tid}")
    public String deleteAnswer(@PathVariable("tid") Integer tid, Model model){
        model.addAttribute("jump","tofaq");
        if (textBiz.deleteByTid(tid)) {
            model.addAttribute("msg","删除回答成功");
        } else {
            model.addAttribute("msg","删除回答失败");
        }
        return "redirect:/jump/tofaq";
    }
    @RequestMapping("collect/{qid}/{index}")
    public String collect(@PathVariable("qid") Integer qid,@PathVariable("index") Integer index,HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        if (user==null){
            model.addAttribute("msg","请登录后再添加至收藏夹");
        } else {
            if (collectBiz.insertCollect(user.getUid(),qid)) {
                model.addAttribute("msg","添加至收藏夹成功");
            } else {
                model.addAttribute("msg","添加至收藏夹失败");
            }
        }
        return index(index,model,session);
    }
}
